# -*- coding: utf-8 -*-
"""
Created on Mon Nov  6 21:35:38 2023

@author: LENOVO
"""
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.metrics import fowlkes_mallows_score
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
from sklearn.metrics import calinski_harabasz_score
from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import classification_report
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error,median_absolute_error,explained_variance_score

winequality = pd.read_csv('数据分析3数据/winequality.csv',sep=';')
wine = pd.read_csv('数据分析3数据/wine.csv')

winequality_data = winequality.iloc[:,:-1]
winequality_target = winequality['quality']

wine_data = wine.iloc[:,:-1]
wine_target = wine['Class']

#将wine_quality数据划分为训练集和测试集
winequality_data_train,winequality_data_test,\
winequality_target_train,winequality_target_test=\
train_test_split(winequality_data,winequality_target,test_size=0.2,random_state=123)

wine_data_train,wine_data_test,\
wine_target_train,wine_target_test=\
train_test_split(wine_data,wine_target,test_size=0.2,random_state=123)

#标准化数据集
stdScaler = StandardScaler().fit(wine_data_train) #生成标准化规则
wine_trainScaler = stdScaler.transform(wine_data_train) #对训练集标准化
wine_testScaler = stdScaler.transform(wine_data_test) #用训练集建立的模型对测试集标准化

Scaler = StandardScaler().fit(winequality_data_train)
winequality_trainScaler = Scaler.transform(winequality_data_train) 
winequality_testScaler = Scaler.transform(winequality_data_test) 

#PCA降维
pca_model = PCA(n_components=5).fit(wine_trainScaler)
wine_train_pca = pca_model.transform(wine_trainScaler)
wine_test_pca = pca_model.transform(wine_testScaler)  

pca_model = PCA(n_components=5).fit(winequality_trainScaler) 
winequality_train_pca = pca_model.transform(winequality_trainScaler)
winequality_test_pca = pca_model.transform(winequality_testScaler) 

#构建K-Means模型
kmeans = KMeans(n_clusters=3,random_state=123).fit(wine_trainScaler) 
KMeans(n_clusters=3, n_init=10,random_state=123)

#对比真实标签和聚类标签求取FMI
score=fowlkes_mallows_score(wine_target_train,kmeans.labels_)
print("wine数据集的FMI:%f"%(score))

#在聚类数目为2~10类时,确定最优聚类数目
for i in range(2,11):
    kmeans = KMeans(n_clusters=i,random_state=123).fit(wine_trainScaler)
    score = fowlkes_mallows_score(wine_target_train,kmeans.labels_)
    print('\nwine为%d类FMI评价分为：%f\n'%(i,score))

#绘制轮廓系数折线图
silhouettteScore = []
for i in range(2,11):
    kmeans = KMeans(n_clusters = i,random_state=1).fit(wine)
    score = silhouette_score(wine,kmeans.labels_)
    silhouettteScore.append(score)
plt.figure(figsize=(10,6))
plt.plot(range(2,11),silhouettteScore,linewidth=1.5, linestyle="-")
plt.show()

#求取Calinski-Harabasz指数
for i in range(2,11):
    kmeans = KMeans(n_clusters = i,random_state=1).fit(wine_trainScaler)
    score =calinski_harabasz_score(wine_trainScaler,kmeans.labels_)
    print('\nseeds数据为%d类calinski_harabaz指数为：%f\n'%(i,score))

wine_quality = pd.read_csv('数据分析3数据/winequality.csv',sep=';')
wine = pd.read_csv('数据分析3数据/wine.csv')

wine_quality_data = wine_quality.iloc[:,:-1]
wine_quality_target = wine_quality['quality']

wine_data = wine.iloc[:,:-1]
wine_target = wine['Class']

#划分训练集、测试集
wine_data_train,wine_data_test,wine_target_train,wine_target_test=\
train_test_split(wine_data,wine_target,test_size=0.2,random_state=123)

#数据标准化
Scaler = MinMaxScaler().fit(wine_data_train)
wine_trainScaler = Scaler.transform(wine_data_train)
wine_testScaler = Scaler.transform(wine_data_test)

#构建SVM分类模型
svm = SVC().fit(wine_trainScaler,wine_target_train)
print("建立的SVM模型为：\n",svm)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)
                                   
#预测训练结果
wine_target_pred = svm.predict(wine_testScaler)   
print('预测前20个结果为：',wine_target_pred[:20])
print('使用SVM预测数据的分析报告为：\n',classification_report(wine_target_test,wine_target_pred))

#构建线性回归模型
clf = LinearRegression().fit(winequality_train_pca,winequality_target_train)
y_pred = clf.predict(winequality_test_pca)
print('线性回归模型预测前10个结果为：','\n',y_pred[:10])

#构建梯度提升回归模型。
gbr_wine = GradientBoostingRegressor().fit(winequality_train_pca,winequality_target_train)
wine_target_pred = gbr_wine.predict(winequality_test_pca)
print('梯度提升回归模型预测前10个结果为：','\n',wine_target_pred[:10])
print('真实标签前10个预测结果为：','\n',list(winequality_target_test[:10]))

print('线性回归模型结果：')
print('winequality数据线性回归模型的均方误差为：',
     mean_squared_error(winequality_target_test,y_pred))
print('winequality数据线性回归模型的中值绝对误差为：',
     median_absolute_error(winequality_target_test,y_pred))
print('winequality数据线性回归模型的可解释方差值为：',
     explained_variance_score(winequality_target_test,y_pred))


print('梯度提升回归模型结果：')
print('winequality数据梯度提升回归树模型的均方误差为：',
     mean_squared_error(winequality_target_test,wine_target_pred))
print('winequality数据梯度提升回归树模型的中值绝对误差为：',
     median_absolute_error(winequality_target_test,wine_target_pred))
print('winequality数据梯度提升回归树模型的可解释方差值为：',
     explained_variance_score(winequality_target_test,wine_target_pred))





